﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.DataVisualization.Charting;
using System.Collections.Generic;
using SilverlightApp.data;
using System.Collections;
using System.Collections.ObjectModel;
using SilverlightApp.Common;
using System.Windows.Threading;

namespace SilverlightApp.Web
{
    public class IlluminationChartBuilder
    {
        private MyObservableCollection<PairInfo> DataSource = null;
        private Chart chart = null;
        private double _WarningMinValue = double.MaxValue;
        private double _WarningMaxValue = double.MinValue;

        public double WarningMinValue { get { return _WarningMinValue; } set { _WarningMinValue = value; } }
        public double WarningMaxValue { get { return _WarningMaxValue; } set { _WarningMaxValue = value; } }

        private DispatcherTimer warningDispatcherTimer = new DispatcherTimer();

        public void BuildChart(Panel panel, MyObservableCollection<PairInfo> ds)
        {
            DataSource = ds;
            chart = new Chart { Title = "光照监控" };
            chart.Height = 230;
            Style legendStyle = new System.Windows.Style();
            Setter legendWidth = new Setter(System.Windows.Controls.Control.WidthProperty, 0);
            Setter legendHeight = new Setter(System.Windows.Controls.Control.HeightProperty, 0);
            legendStyle.Setters.Add(legendWidth);
            legendStyle.Setters.Add(legendHeight);
            legendStyle.TargetType = typeof(System.Windows.Controls.Control);
            chart.LegendStyle = legendStyle;

            DataPointSeries series = new ColumnSeries();
            series.MaxHeight = 250;
            series.ItemsSource = ds;
            //series.Title = 0 + " lx";

            series.IndependentValueBinding = new System.Windows.Data.Binding("Name");
            series.DependentValueBinding = new System.Windows.Data.Binding("Value");
            Style lineStyle = new System.Windows.Style();
            lineStyle.TargetType = typeof(System.Windows.Controls.Control);
            Setter setterRed = new Setter(System.Windows.Controls.Control.BackgroundProperty, new SolidColorBrush(Color.FromArgb(255, 0, 255, 0)));
            lineStyle.Setters.Add(setterRed);
            series.DataPointStyle = lineStyle;
            series.Width = 100;
            LinearAxis valueAxis = new LinearAxis { Orientation = AxisOrientation.Y, Minimum = 0, Maximum = 150, ShowGridLines = true };
            valueAxis.Interval = 30;

            warningDispatcherTimer.Interval = TimeSpan.FromSeconds(0.3);
            warningDispatcherTimer.Tick += new EventHandler(warningDispatcherTimer_Tick);

            ds.AfterPropertyChanged = (self) =>
            {
                double value = Math.Round(self[0].Value, 2);
                if (self != null && self.Count > 0)
                {
                    //series.Title = value + " lx";
                    chart.Title = string.Format("光照监控" + Environment.NewLine + "({0}lx)", value);
                }
                valueAxis.Interval = (int)(((valueAxis.Maximum.Value - valueAxis.Minimum.Value) / 6 + 10) / 10) * 10;
                if (valueAxis.Maximum.Value < value)
                {
                    valueAxis.Maximum = value + 10;
                }

                if (self[0].Value < WarningMinValue || self[0].Value > WarningMaxValue)
                {
                    StartDispatcherTimer();
                }
                else
                {
                    EndDispatcherTimer();
                }
            };
            chart.Axes.Add(valueAxis);
            chart.Series.Add(series);
            SeriesTemp = series;
            Grid grid = new Grid();
            grid.Children.Add(chart);
            panel.Children.Add(grid);
        }

        private static DataPointSeries SeriesTemp = null;
        private void StartDispatcherTimer()
        {
            if (warningDispatcherTimer.IsEnabled)
            {
                return;
            }
            warningDispatcherTimer.Start();
        }

        private void EndDispatcherTimer()
        {
            if (warningDispatcherTimer.IsEnabled)
            {
                warningDispatcherTimer.Stop();
                DataPointSeries series = SeriesTemp;
                Style tempLineStyle = new System.Windows.Style();
                tempLineStyle.Setters.Add(new Setter(System.Windows.Controls.Control.BackgroundProperty, new SolidColorBrush(Color.FromArgb(255, 0, 255, 0))));
                series.DataPointStyle = tempLineStyle;
                
            }
        }

        private bool isOp = false;
        void warningDispatcherTimer_Tick(object sender, EventArgs e)
        {
            DataPointSeries series = SeriesTemp;
            Style tempLineStyle = new System.Windows.Style();
            tempLineStyle.TargetType = typeof(System.Windows.Controls.Control);
            Setter setterRed = null;

            if (isOp)
            {
                setterRed = new Setter(System.Windows.Controls.Control.BackgroundProperty, new SolidColorBrush(Color.FromArgb(255, 0, 255, 0)));
            }
            else
            {
                setterRed = new Setter(System.Windows.Controls.Control.BackgroundProperty, new SolidColorBrush(Color.FromArgb(125, 0, 255, 0)));
                   // new Setter(System.Windows.Controls.Control.BackgroundProperty, new SolidColorBrush(Color.FromArgb(120, 0, 255, 0)));
            }

            tempLineStyle.Setters.Add(setterRed);
            isOp = !isOp;
            series.DataPointStyle = tempLineStyle;
        }
    }
}
